package com.zxy;

import cn.hutool.core.date.DateUtil;
import com.zxy.config.annotation.Doc;
import com.zxy.domain.WebUrl;
import com.zxy.mapper.WebUrlMapper;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

@SpringBootApplication
@MapperScan({"com.zxy.mapper","com.zxy.dept.mapper"})
@Slf4j
@EnableAsync
public class CustomerApplication {

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Resource
    private WebUrlMapper webUrlMapper;

    @PostConstruct
    @Async("nativeAsyncTaskPool")
    public void getAllWebUrl(){
        log.info("初始化前时间：【{}】",DateUtil.now());
        // 启动前清空web-url表数据
        webUrlMapper.truncateWebUrlTable();
        log.info("-----系统启动，自动获取当前所有url加入本地缓存，当前时间：【{}】", DateUtil.now());
        Map<RequestMappingInfo, HandlerMethod> map = webApplicationContext.getBean(RequestMappingHandlerMapping.class).getHandlerMethods();
        CopyOnWriteArrayList<WebUrl> list = new CopyOnWriteArrayList<>();
        map.forEach((k,v)->{
            WebUrl webUrl = new WebUrl();
            // 判断是自己的控制器还是spring官方的控制器
            if (v.toString().startsWith("com.zxy")) {
                // 反射拿到当前类的所有方法
                Doc doc = v.getMethodAnnotation(Doc.class);
                if (doc!=null){
                    webUrl.setDoc(doc.doc());
                }else {
                    webUrl.setDoc("该接口没有标识用途");
                }
                RequestMethodsRequestCondition methodsCondition = k.getMethodsCondition();
                RequestMethod method = new ArrayList<>(methodsCondition.getMethods()).get(0);
                PatternsRequestCondition condition = k.getPatternsCondition();
                // 有可能新建的项目没有所有待候选的url，这里不这样判断就会抛出异常
                if (condition==null){
                    return;
                }
                String p = new ArrayList<>(condition.getPatterns()).get(0);
                webUrl.setUrl(p);
                webUrl.setMethod(method.name());
                webUrl.setEnable(true);
                list.add(webUrl);
            }
        });
        webUrlMapper.addWebUrl(list);
        log.info("初始化后时间：【{}】",DateUtil.now());
    }

    public static void main(String[] args) {
        SpringApplication.run(CustomerApplication.class, args);
    }

}
